<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class model_attach extends base_model{

	//hook mod_attach_start.php
	private $table;
	private $now;

	function __construct() {
		//hook mod_attach_init_init_before.php
		parent::__construct();
		$this->table = 'attach';
		//hook mod_attach_init_init_end.php
	}
	
	public function attach_add($uid, $path = '', $md5 = '',$infos = '[]') {
		$aid = $this->db->maxid($this->table.'-aid', '+1');
		//hook mod_attach_attach_add.php
		return $this->db->set( $this->table.'-aid-'.$aid, 
					array(	'aid'=>$aid,'md5'=>$md5,'uid'=>$uid,
							'path'=>$path,'infos'=>$infos));
	}

	public function attach_edit($md5, $tid) {
		//hook mod_attach_attach_edit.php
		return $this->db->update( $this->table.'-md5-'.$md5, 
					array(	'tid'=>$tid));
	}
	
	public function attach_del($md5) {
		//hook mod_attach_attach_del.php
		$arr = $this->db->get($this->table.'-md5-'.$md5);
		if(!empty($arr)){
			//hook mod_attach_attach_del_before_del.php
			$tmppath = str_replace(CONFIG_UPS_PATH,CONFIG_THU_PATH,$arr['path']);
			if(file_exists(IN_APP. $tmppath))
				@unlink($tmppath);
			@unlink($arr['path']);
			$this->db->delete( $this->table.'-md5-'.$md5);
		}
		return TRUE;
	}

	public function attach_list_untarget($start, $limit) {

		//hook mod_attach_attach_list_untarget.php
		return $this->db->index_fetch($this->table , 'aid',
						array('tid'=>0, 'lasttime'=>array('<'=>$this->now) ),
						array('aid'=>1) , $start, $limit);

	}
	
	public function attach_count_untarget() {

		//hook mod_attach_attach_count_untarget.php
		//为了同步时间
		$this->now = date('Y-m-d H:i:s',time() - 1800);	
		return $this->db->index_count($this->table ,
						array('tid'=>0, 'lasttime'=>array('<'=>$this->now) ));
	}

	//hook mod_attach_end.php
}
/*{TranslateTagEnd}*/

?>